<template>
  <div class="search-result">
    <!-- 导航栏 -->
    <van-nav-bar
      :title="$route.query.keyword + '的搜索结果'"
      left-arrow
      fixed
      @click-left="$router.back()"
    />
    <!-- /导航栏 -->

    <!-- 文章列表 -->
    <van-list
      v-model="loading"
      class="article-list"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="item in list"
        :key="item.art_id"
        :title="item.title"
        @click="$router.push('/article/' + item.art_id)"
      />
    </van-list>
    <!-- /文章列表 -->
  </div>
</template>

<script>
import { reqGetSearch } from '@/api/search.js'
export default {
  name: 'SearchResult',
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1
    }
  },

  methods: {
    async onLoad() {
      // 异步更新数据
      const keyword = this.$route.query.keyword
      const { data: { results }} = await reqGetSearch(keyword, this.page)
      this.list = [...this.list, ...results]
      this.page++
      // 加载状态结束
      this.loading = false
      // 数据全部加载完成
      if (results.length === 0) {
        this.finished = true
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.search-result {
  .article-list {
    margin-top: 39px;
  }
  ::v-deep {
    .van-nav-bar {
      .van-icon {
        color: white;
      }
    }
  }
}
</style>
